package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysRoleSvc;
import com.kmxd.ams.client.system.dto.*;
import com.kmxd.ams.client.system.vo.SysMenuVO;
import com.kmxd.ams.client.system.vo.SysRoleListVO;
import com.kmxd.ams.client.system.vo.SysRoleVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色表 前端控制器
 *
 * @author admin
 * @since 2021-06-10
 */
@RestController
@Tag(name = "角色管理")
@RequestMapping("/system/sysRole")
@AllArgsConstructor
public class SysRoleCtl {

  private ISysRoleSvc sysRoleSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysRoleListVO>> selectPage(Page reqPage, SysRoleQueryDTO req) {
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    return R.success(sysRoleSvc.selectPage(reqPage, req));
  }

  /** 角色列表 */
  @Operation(summary = "角色列表")
  @GetMapping("/list")
  public R<List<SysRoleListVO>> selectList(SysRoleQueryDTO req) {
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    return R.success(sysRoleSvc.selectList(req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysRole:add")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysRoleAddDTO req) {
    sysRoleSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysRole:edit")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysRoleEditDTO req) {
    sysRoleSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysRole:remove")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysRoleSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysRoleVO> view(@RequestParam Long id) {
    return R.success(sysRoleSvc.view(id));
  }

  @GetMapping("/export")
  @SaCheckPermission("system:sysRole:export")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(SysRoleQueryDTO req, HttpServletResponse response) {
    String filename = "角色列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(SysRoleListVO.class)
        .sheet("角色列表")
        .doWrite(sysRoleSvc.selectList(req));
  }

  /** 查询下拉框列表 */
  @GetMapping("/listOptions")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询下拉框列表")
  public R<List<SysRoleVO>> listOptions() {
    return R.success(sysRoleSvc.listOptions());
  }

  /** 分配菜单 */
  @PostMapping("/assignMenu")
  @SaCheckPermission("system:sysRole:assignMenu")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分配菜单")
  public R<Void> assignMenu(@Validated @RequestBody SysRoleAssignMenuDTO req) {
    sysRoleSvc.assignMenu(req);
    return R.success();
  }

  /** 分配档案库 */
  @PostMapping("/assignArchive")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分配档案库")
  public R<Void> assignArchive(@Validated @RequestBody List<SysRoleAssignArchiveDTO> req) {
    sysRoleSvc.assignArchive(req);
    return R.success();
  }

  /** 查询角色关联的菜单 */
  @GetMapping("/listRoleMenus")
  @AutoLog(
      module = "角色管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询角色关联的菜单")
  public R<List<SysMenuVO>> listRoleMenus(@RequestParam String roleId) {
    return R.success(sysRoleSvc.listRoleMenus(roleId));
  }

  /** 获取指定角色菜单ids列表 */
  @GetMapping("/listRoleMenuIds")
  @AutoLog(
      module = "获取指定角色菜单ids列表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询角色关联的菜单")
  public R<Set<String>> listRoleMenuIds(@RequestParam String roleId) {
    return R.success(sysRoleSvc.listRoleMenuIds(roleId));
  }

  /** 分配用户 */
  @PostMapping("/assignUsers")
  @SaCheckPermission("system:sysRole:assignUsers")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分配用户")
  public R<Void> assignUsers(@Validated @RequestBody SysRoleAssignUsersDTO req) {
    sysRoleSvc.roleAssignUsers(req);
    return R.success();
  }

  /** 下拉列表 */
  @GetMapping("/selectOptions")
  @AutoLog(module = "角色管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "下拉列表")
  public R<List<SysRoleVO>> selectOptions() {
    return R.success(sysRoleSvc.selectOptions());
  }
}
